package com.ecric.http.OkHttp3.package3;

import java.io.ObjectStreamException;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 *
 * 针对OKHttp框架进行简单的封装
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> luzhenbang
 * <h3>Date</h3> 04/04/2018 14:35
 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL  Co., Ltd. Inc. All rights reserved.
 */
public class OkHttpsUtil {


    private  OkHttpClient httpClient;

    /**
     * 构造方法私有化
     */
    private OkHttpsUtil (){
        httpClient = new OkHttpClient
                .Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10,TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
//                .cache(cache)
                .build();
    }


    public OkHttpClient getHttpClient() {
        if (httpClient == null){
            httpClient = new OkHttpClient
                    .Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10,TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
//                .cache(cache)
                    .build();
        }
        return httpClient;
    }

    /**
     * 单例模式
     * @return
     */
    public static OkHttpsUtil getInstance() {
        return OkHttpsUtilHelp.okHttpsUtil;
    }


    public PostFormBuilder post(){
        return new PostFormBuilder();
    }


    /**
     * 静态内部类实现单例模式
     */
    private static class  OkHttpsUtilHelp{
        private static  OkHttpsUtil  okHttpsUtil = new OkHttpsUtil();
    }


    private Object readResolve() throws ObjectStreamException {
        return getInstance();
    }




}
